package com.huike.clues.service.impl;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @author Mr.Chen
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Resource
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Resource
    private TbClueMapper tbClueMapper;

    /**
     * 添加跟进记录
     * @param tbClueTrackRecord 展示到前端页面的数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = BaseException.class)
    public Integer add(ClueTrackRecordVo tbClueTrackRecord) {

        //创建线索对象
        TbClue tbClue = new TbClue();
        //copy数据
        BeanUtils.copyBeanProp(tbClue, tbClueTrackRecord);
        //更新id
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClue.setCreateBy(SecurityUtils.getUsername());

        tbClueMapper.updateTbClue(tbClue);

        //添加跟进记录
        TbClueTrackRecord record = new TbClueTrackRecord();
        record.setCreateBy(SecurityUtils.getUsername());
        BeanUtils.copyBeanProp(record,tbClueTrackRecord);

        Integer rows = tbClueTrackRecordMapper.add(record);


        return rows;
    }

    /**
     *查询线索跟进记录列表
     * @param clueId 线索id
     * @return 返回分页数据
     */
    @Override
    public List<TbClueTrackRecord> selectTbClueTrackRecordList(Long clueId) {

        List<TbClueTrackRecord> tbClueTrackRecordList = tbClueTrackRecordMapper.getTbClueTrackRecordList(clueId);

        return tbClueTrackRecordList;
    }
}
